﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Expo.Training.Entity;
using System.Text.RegularExpressions;
using Expo.Training.BRule.Member;
using Expo.Training.BRule;
using System.IO;
using Expo.Training.BRule.Audit;
using System.Web.Script.Serialization;

public partial class BM_Register : Pagebase
{
    private Guid rootDepartmentID = Guid.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.rootDepartmentID = GetRootDepartmentID();
        if (!IsPostBack)
        {
            BindMember();
            BindCityList();
        }
    }

    private void BindMember()
    {
        //检查 Customer 设置
        Customer_Info objCI = new Customer_Info();
        objCI.CustomerID = this.Customer.CustomerID;
        if (objCI.Exist())
        {
            if (objCI.IsSignUp != 1)
            {
                objCI.Dispose();
                this.dv_NoResult.Visible = true;
                this.dv_Result.Visible = false;
                //不允许注册
            }
            else
            {
                objCI.Dispose();
                this.dv_NoResult.Visible = false;
                this.dv_Result.Visible = true;
            }

            ////判断注册数量是否大于设置的数量
            //Customer_Access objCA = new Customer_Access();
            //objCA.CustomerID = this.Customer.CustomerID;
            //if (objCA.Exist())
            //{
            //    //用户许可人数已满！不能注册
            //    if (objCA.AccountLicense != 0 && objCA.AccountLicense <= objCA.UseAccountLicence)
            //    {
            //        objCA.Dispose();
            //        response.Write("4");
            //        return;
            //    }
            //}
        }
    }

    //获取99xb的根部门，所有用户都在根部门下（都属于慈溪市教育局）
    private Guid GetRootDepartmentID()
    {
        Guid rootID = Guid.Empty;
        Department_Info di = new Department_Info();
        di.CustomerID = this.Customer.CustomerID;
        di.ParentID = Guid.Empty;
        if (di.Exist())
        {
            rootID = di.DepartmentID;
        }
        return rootID;
    }


    private void BindCityList()
    {
        this.ddl_WordAddress_Province.DataSource = BindCityDate(1, 0);
        this.ddl_WordAddress_Province.DataTextField = "AreaName";
        this.ddl_WordAddress_Province.DataValueField = "AreaId";
        this.ddl_WordAddress_Province.DataBind();
        this.ddl_WordAddress_Province.Items.Insert(0, new ListItem("  ---  请选择  ---  ", "0"));

        this.ddl_HomeAddress_Province.DataSource = BindCityDate(1, 0);
        this.ddl_HomeAddress_Province.DataTextField = "AreaName";
        this.ddl_HomeAddress_Province.DataValueField = "AreaId";
        this.ddl_HomeAddress_Province.DataBind();
        this.ddl_HomeAddress_Province.Items.Insert(0, new ListItem("  ---  请选择  ---  ", "0"));
    }

    private DataTable BindCityDate(int nodeType, int areaKey)
    {
        DataTable objData = new DataTable();
        City_Info objCity = new City_Info();
        objCity.NodeType = nodeType;
        objCity.AreaKey = areaKey;
        objCity.OrderBy = "Px";
        objData = objCity.QueryDataTable();
        objCity.Dispose();
        return objData;
    }

    #region 添加用户信息
    //Modify 2014-10-14
    //增加数据同步接口
    protected void but_Add_Click(object sender, EventArgs e)
    {
        
        
        int AuditStatus = 0;
        Customer_Info objCI = new Customer_Info();
        objCI.CustomerID = this.Customer.CustomerID;
        if (objCI.Exist())
        {
            AuditStatus = objCI.IsAudit;
        }
        objCI.Dispose();
        int wordAddress_Province = Convert.ToInt32(this.ddl_WordAddress_Province.SelectedValue.ToString());
        int wordAddress_City = Convert.ToInt32(this.hd_WordAddress_City.Value.ToString());
        int wordAddress_Area = Convert.ToInt32(this.hd_WordAddress_Area.Value.ToString());

        int homeAddress_Province = Convert.ToInt32(this.ddl_HomeAddress_Province.SelectedValue.ToString());
        int homeAddress_City = Convert.ToInt32(this.hd_HomeAddress_City.Value.ToString());
        int homeAddress_Area = Convert.ToInt32(this.hd_HomeAddress_Area.Value.ToString());

        string nickname = this.txt_NickName.Value.Trim();
        string useraccount = this.txt_useraccount.Value.Trim();
        string password = this.txt_pwd.Value.Trim();
        string password2 = this.txt_repwd.Value.Trim();
        string cellphone = this.txt_cellphone.Value.Trim();
        string email = this.txt_email.Value.Trim();
        string realname = this.txt_realname.Value.Trim();
        string checkcode = CheckCode.Text.Trim();

        sp_UserAccount.Attributes.Add("style", "display:none");
        sp_UserAccount2.Attributes.Add("style", "display:none");
        sp_NickName.Attributes.Add("style", "display:none");
        sp_NickName2.Attributes.Add("style", "display:none");
        sp_Password2.Attributes.Add("style", "display:none");
        sp_Password3.Attributes.Add("style", "display:none");
        sp_Password4.Attributes.Add("style", "display:none");
        sp_Password5.Attributes.Add("style", "display:none");
        sp_IDCard.Attributes.Add("style", "display:none");
        //sp_Email.Attributes.Add("style", "display:none");
        sp_Email2.Attributes.Add("style", "display:none");
        sp_Email3.Attributes.Add("style", "display:none");
        sp_Realname.Attributes.Add("style", "display:none");

        spCardID.Attributes.Add("style", "display:none");

        if (useraccount.Length == 0)
        {
            sp_UserAccount.Attributes.Add("style", "display:''");
            AddEndScript("unsuccessful", "alert('用户名不能为空！');");
            return;
        }
        else
        {
            Member_Info objM = new Member_Info();
            objM.UserAccount = useraccount;
            objM.CustomerID = this.Customer.CustomerID;
            if (objM.Exist())
            {
                this.sp_UserAccount2.Attributes.Add("style", "display:''");
                return;
            }
        }
        if (nickname.Length == 0)
        {
            sp_NickName.Attributes.Add("style", "display:''");
            AddEndScript("unsuccessful", "alert('昵称不能为空！');");
            return;
        }
        else
        {
            Member_Info objM = new Member_Info();
            objM.NickName = nickname;
            objM.CustomerID = this.Customer.CustomerID;
            if (objM.Exist())
            {
                this.sp_NickName2.Attributes.Add("style", "display:''");
                return;
            }
        }
        if (password.Length == 0)
        {
            sp_Password5.Attributes.Add("style", "display:''");
            return;
        }
        else if (password.Length > 0 && password.Length < 8)
        {
            sp_Password4.Attributes.Add("style", "display:''");
            //AddEndScript("wrong", "alert('密码必须至少8位字符！');");
            return;
        }
        if (password != password2)
        {
            sp_Password2.Attributes.Add("style", "display:''");
            return;
        }

        if (realname.Length == 0)
        {
            sp_Realname.Attributes.Add("style", "display:''");
            //AddEndScript("unsuccessful", "alert('真实姓名不能为空！');");
            return;
        }

        if (email.Length > 0)
        {
            //    sp_Email.Attributes.Add("style", "display:''");
            //    AddEndScript("unsuccessful", "alert('E-Mail不能为空！');");
            //    return;
            //}
            //else
            //{
            Regex gRegPostCode = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            if (!gRegPostCode.IsMatch(email))
            {
                sp_Email2.Attributes.Add("style", "display:''");
                return;
            }
            else
            {
                Member_Info objM = new Member_Info();
                objM.CustomerID = this.Member.CustomerID;
                objM.Email = email;
                if (objM.Exist())
                {
                    sp_Email3.Attributes.Add("style", "display:''");
                    return;
                }
            }
        }
        else
        {
            //sp_Email.Attributes.Add("style", "display:''");
            AddEndScript("unsuccessful", "alert('E-Mail不能为空！');");
            return;
        }

        if (ddl_CardType.SelectedValue.Equals("1"))
        {
            if (this.txt_IDCard.Value.Length != 0)
            {
                //Regex reg = new Regex(@"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                //if (!reg.IsMatch(this.txt_IDCard.Value))
                //{
                //    this.AddEndScript("wrong", "alert('身份证号码输入不正确！')");
                //    return;
                //}
                string chkMsg = PublicFunction.CheckCidInfo(this.txt_IDCard.Value).ToUpper();
                if (!chkMsg.Contains("CHKPASS"))
                {
                    this.AddEndScript("wrong", "alert('身份证号码输入不正确！" + chkMsg + "')");
                    return;
                }
                else
                {
                    Member_Info objMem = new Member_Info();
                    objMem.IDCard = this.txt_IDCard.Value;
                    objMem.CustomerID = this.Customer.CustomerID;
                    if (objMem.Exist())
                    {
                        objMem.Dispose();
                        this.AddEndScript("wrong1", "alert('身份证号码已存在！')");
                        return;
                    }
                    else
                    {
                        objMem.Dispose();
                        objMem = new Member_Info();

                        objMem.IDCard = VerifyIDCardNumber.GetAnotherID(this.txt_IDCard.Value);
                        objMem.CustomerID = this.Customer.CustomerID;
                        if (objMem.Exist())
                        {
                            objMem.Dispose();
                            this.AddEndScript("wrong1", "alert('对应15或18号身份证号码已存在！')");
                            return;
                        }
                    }
                    objMem.Dispose();
                }
            }
            else
            {
                sp_IDCard.Attributes.Add("style", "display:''");
                return;
            }
        }

        //2013.3.8验证学习卡
        string cardID = txt_CardID.Value;
        if (cardID.Trim().Length != 0)
        {
            MemberCard_Info mci = new MemberCard_Info();
            mci.CardID = cardID;
            if (mci.Exist())
            {
                this.AddEndScript("wrong1", "alert('学习卡号码已存在！')");
                mci.Dispose();
                return;
            }
        }

        if (!checkcode.Equals(Session["Vnumber"].ToString().ToLower()))
        {
            AddEndScript("fail", "alert('验证码不正确，请重新输入！');");
            return;
        }

        Member_Info objMember = new Member_Info();
        objMember.MemberID = Guid.NewGuid();
        objMember.UserAccount = useraccount;
        objMember.NickName = nickname;
        objMember.CustomerID = this.Customer.CustomerID;
        objMember.RealName = realname;
        if (this.rbtn_male.Checked)
            objMember.Sex = 0;
        if (this.rbtn_female.Checked)
            objMember.Sex = 1;
        objMember.Duty = txt_Duty.Value.Trim();
        objMember.CardType = Int32.Parse(ddl_CardType.SelectedValue);
        objMember.IDCard = txt_IDCard.Value.Trim();
        objMember.Email = email;
        objMember.CellPhone = cellphone;
        if (AuditStatus != 0)
        {
            objMember.Stutas = MemberConst.MEMBER_STATUS_ADDNOTAUDIT; //需要审核
        }
        else
        {
            objMember.Stutas = MemberConst.MEMBER_STATUS_START;  //正常，无需审核直接通过
        }
        objMember.WorkAddress_Province = wordAddress_Province;
        objMember.WorkAddress_City = wordAddress_City;
        objMember.WorkAddress_Area = wordAddress_Area;
        objMember.HomeAddress_Province = homeAddress_Province;
        objMember.HomeAddress_City = homeAddress_City;
        objMember.HomeAddress_Area = homeAddress_Area;
        if (password.Length > 0)
            objMember.Password = PublicFunction.EnPassword(password);
        else
            objMember.Password = PublicFunction.EnPassword("12345678");
        //objMember.CustomerID = this.Member.CustomerID;
        DateTime periodDate = DateTime.Parse("1900-1-1");
        objMember.PeriodDate = periodDate;
        objMember.HeaderUrl = "";
        objMember.Creator = this.Member.MemberID;

        if (chkIsVolunteer.Checked)
        {
            objMember.Volunteer = 1;
        }
        else
        {
            objMember.Volunteer = 0;
        }

        if (objMember.Add())
        {
            Member_Detail objDetail = new Member_Detail();
            objDetail.MemberID = objMember.MemberID;
            objDetail.Add();
            // 新增该用户的用户标签
            Expo.Training.BRule.Exam.QuestionManager.AddAttributes(objMember.MemberID, ta_Attributes.SelectedValue);

            //2013.3.8添加学习卡信息
            MemberCard_Info mci = new MemberCard_Info();
            mci.MemberID = objMember.MemberID;
            mci.CardID = cardID;
            if (!mci.Exist())
            {
                mci.MemberID = objMember.MemberID;
                mci.CardID = cardID;
                mci.Add();
            }
            mci.Dispose();

            Member_Department objMD = new Member_Department();
            objMD.MemberID = objMember.MemberID;
            //objMD.DepartmentID = this.rootDepartmentID;
            
            Department_Info objDI = new Department_Info();
            objDI.DepartmentName = "巴马县教育局";
            if (objDI.Exist())
            {
                objMD.DepartmentID = objDI.DepartmentID;
            }

            if (objMD.Add())
            {
                //Modify 2014-10-14
                //增加数据同步接口
                if (MemberAccess.AddMemberToBank("", objMember.MemberID.ToString(), objMember.UserAccount, objMember.Password, objMember.RealName, "", objMember.IDCard, "", "", "", "") == "Fail")
                {
                    AddEndScript("add succes", "alert('数据同步失败！请联系管理员!');");
                }
                
                Audit.AuditLog(this.Member.MemberID, string.Format("{0}|{1}|{2},{3},{4}|{5}", "用户信息录入", "添加", objMember.UserAccount, objMember.RealName, "已录入", "成功"));
                
                AddEndScript("add succes", "alert('添加用户成功！');location.href='Default.aspx'");
            }
        }
    }
    #endregion
}
